import PAGE from '../../../utils/middleware/page'
import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import store from '../../../store/index'
Page(PAGE({
    behaviors: [storeBindingsBehavior],
    storeBindings: {
        store,
        actions: {
            updateNeedRefresh: 'vehicle.updateNeedRefresh'
        }
    },
    data: {
        userInfo: null,
        apiField: {
            module: 'member',
            api: 'GetLoginUser',
            data: 'userInfo',
            search: {}
        },
        files: [],
        faceFeature: null,
        faceBase64String: null
    },
    onLoad() {
        this.fetchData()
    },
    async fetchData() {
        this.handleFetchData({
            complete: (data) => {
                if (data.FaceUrl) {
                    this.setData({
                        files: [{ url: data.FaceUrl }]
                    })
                }
            }
        })
    },
    async selectFile(e) {
        var file = e.detail
        try {
            let imgBase64 = wx.getFileSystemManager().readFileSync(file.tempFilePaths[0], "base64")
            const res = await wx.$Api.face.Check({
                data: {
                    ExcludeId: 'true',
                    FaceBase64String: imgBase64
                },
                hasLoading: true
            })
            this.data.faceFeature = res.Data.FaceFeature
            this.data.faceBase64String = 'data:image/jpg;base64,' + res.Data.FaceBase64String
            this.setData({
                files: [{ url: file.tempFilePaths[0] }]
            })
        }
        catch (e) {
            wx.$Fun.showToast(e.Message)
        }
    },
    uploadError(e) {
    },
    deleteFile(e) {
        var index = e.detail.index
        const files = this.data.files
        files.splice(index, 1)
        this.setData({
            files
        })
        this.data.faceFeature = null
        this.data.faceBase64String = null
    },
    async submitForm(e) {
        const params = e.detail.value
        if (!params.Name) {
            wx.$Fun.showToast('姓名不能为空')
            return
        }
        try {
            await wx.$Api.member.UpdateInfo({
                data: {
                    Name: params.Name,
                    FaceBase64String: this.data.faceBase64String,
                    FaceFeature: this.data.faceFeature
                }
            })

            this.updateNeedRefresh(true)
            //跳转导首页
            wx.$Fun.toPage({
                page: 'mine',
                params: {
                    pageType: 'switchTab'
                }
            })
        }
        catch (e) {
            wx.$Fun.showToast(e.Message)
        }
    },
}))